package com.javatip.project.deep_learning.YoloValidator;

import org.apache.poi.ss.formula.functions.T;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class YoloValidator {

    public static void main(String[] args) {
        // 定义 YOLO 命令
        String[] command = {
                "yolo",
//                "/home/jolly/develop/PycharmProjects/YOLO/ultralytics/",
                "task=detect",
                "mode=predict",
//                "split=val",
                "model=/home/jolly/develop/PycharmProjects/YOLO/ultralytics/results/2024-09-08_21-06-31/weights/best.pt",
//                "data=/home/jolly/develop/PycharmProjects/YOLO/ultralytics/ultralytics/cfg/datasets/elevator.yaml",
                "source=/home/jolly/develop/PycharmProjects/YOLO/ultralytics/datasets/aaaa/images/val/",
                "project=/home/jolly/develop/project/java/detection/src/main/resources/predictResult/"
        };

        // 设置工作目录
//        File workingDirectory = new File("/home/jolly/develop/PycharmProjects/YOLO/ultralytics/");
        // 创建 ProcessBuilder 对象
        ProcessBuilder processBuilder = new ProcessBuilder(command);
//        processBuilder.directory(workingDirectory);  // 设置工作目录
        processBuilder.redirectErrorStream(true); // 将错误流重定向到标准输出

        try {
            // 启动进程
            Process process = processBuilder.start();

            // 读取并打印输出
            new Thread(() -> {
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                    String line;
                    while ((line = reader.readLine()) != null) {
                        System.out.println(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();

            // 等待进程结束
            int exitCode = process.waitFor();
            System.out.println("Process exited with code: " + exitCode);

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
